package com.chima.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.chima.dao.PerformanceDao;
import com.chima.domain.Employees;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Performance;
import com.chima.domain.Rate;
import com.chima.service.PerformanceService;

public class PerformanceImp  implements PerformanceService{
	private PerformanceDao performanceDao;
	
	public PerformanceDao getPerformanceDao() {
		return performanceDao;
	}
	public void setPerformanceDao(PerformanceDao performanceDao) {
		this.performanceDao = performanceDao;
	}
	
	//奖金计算
	public PageResponseBean findPerformance(PageRequestBean pageRequestBean) {
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total = performanceDao.findByTotalCount(pageRequestBean.getDate(),pageRequestBean.getDept());
		// 获取分页查询条件
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Performance.class);
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<Performance> performances = performanceDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(performances);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}
	public void updatePerformance(Performance performance) {
		performanceDao.update(performance);
		
	}
	public void savePerformance(Performance performance) {
		performanceDao.save(performance);
	}
	public void addBatch(Performance performance) {
		performanceDao.update(performance);
}
//	public List<Performance> findByDateAndDepart( String date, int depart) {
//		return performanceDao.findByDateAndDepart(date, depart);
//	}
//	public Double findMaxTotalCount(String date, int depart) {
//		return performanceDao.findMaxTotalCount(date, depart);
//		
//	}
//	public Double findMinTotalCount(String date, int depart) {
//		return performanceDao.findMinTotalCount(date, depart);
//		
//	}
	public void updateTotalTime(Double totaltime, Employees employee) {
		performanceDao.updateTotalTime(totaltime, employee);
		
	}
//	public void updateFen(int chengxu, Double JINJIZHYAO, Double JINJIBUZHYAO,Double BUJINZHYAO,
//			Double BUJINBUZHYAO,Double CHUANGXIN,Double totalCount,Employees employee,int yiji,int erji,int sanji, int siji) 
//	{
//		performanceDao.updateFEN(chengxu, JINJIZHYAO, JINJIBUZHYAO, BUJINZHYAO, BUJINBUZHYAO, CHUANGXIN,totalCount,employee,yiji, erji, sanji, siji);
//		
//	}
	public void updateFen(int chengxu, Double JINJIZHYAO, Double JINJIBUZHYAO,Double BUJINZHYAO,
			Double BUJINBUZHYAO,Double totalCount,Employees employee,int yiji,int erji,int sanji, int siji) 
	{
		performanceDao.updateFEN(chengxu, JINJIZHYAO, JINJIBUZHYAO, BUJINZHYAO, BUJINBUZHYAO,totalCount,employee,yiji, erji, sanji, siji);
		
	}
	public List<Rate> findRateByDateAndDepart(String date, int depart) {
		
		return performanceDao.findRateByDateAndDepart(date, depart);
	}
	public void updateTotalTime(Double totaltime, int id) {
		performanceDao.updateTotalTime(totaltime, id);
		
	}
	
	
	//奖金查询
	public PageResponseBean countPerformance(PageRequestBean pageRequestBean) {
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total = performanceDao.findTotalCount(pageRequestBean.getDate(),pageRequestBean.getDept());
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<Performance> performances = performanceDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(performances);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}
	
	public List<Performance> findByDateAndDepartment(String date, int depart) {
		return performanceDao.findByDateAndDepartment(date, depart);
	}
	public Performance findById(int id) {
		return performanceDao.findById(id);
	}
	public void saveOrupdatePerformance(Employees  employee,double totalcoffer,double empcoffe, String date,double reward) {
		Performance  p=performanceDao.findByDateAndEmp(date, employee.getId());
		if(p==null){
			Performance pp=new Performance();
			pp.setEmployee(employee);
			pp.setDepartment(employee.getDepartment());
			pp.setDate(date);
			pp.setReward(empcoffe/totalcoffer*reward);
			performanceDao.save(pp);
		}else{
			p.setReward(empcoffe/totalcoffer*reward);
			performanceDao.update(p);
		}
		
		
	}
	public PageResponseBean findEmpPerformance(PageRequestBean pageRequestBean) {
	
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total = performanceDao.findByTotalCount(pageRequestBean.getDate(),pageRequestBean.getDept());
		// 获取分页查询条件
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Performance.class);
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<Performance> performances = performanceDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(performances);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}
	public void DeletePerformance(int empid) {
		
		performanceDao.DeletePerformance(empid);
	}
	public List<Performance> findByDate(String date) {
		
		return performanceDao.findByDate(date);
	}
	@Override
	public List<Performance> findallEmpPerformanceBydate(String searchdate) {
		// TODO Auto-generated method stub
		return performanceDao.findallEmpPerformanceBydate(searchdate);
	}
	

}
